import { Modal } from 'view-design'
import {chooseUrl} from '@/api/sysSetting.js'
import util from "@/libs/util"
import cookie from "../util.cookies"

//保存pdf
const savePdfFn = (text) => {

}
//保存excel
const saveExcelFn = (text, listQuery, url,_time) => {
    var _content = ""
    if(_time && _time !== 0){
        _content = "<p>确认导出为Excel格式？</p><p>预计"+_time+"分钟导出成功</p>"
    }else{
        _content = "<p>确认导出为Excel格式？</p>"
    }
  Modal.confirm({
    title: '温馨提示',
    content: _content,
    loading: true,
    onOk: () => {
      fetch(
        chooseUrl(url), {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
            Authorization: cookie.get('access_token')
          },
          body: JSON.stringify({ ...listQuery })
        }
      )
        .then(res => res.blob())
        .then(data => {
          Modal.remove()
          util.download(data,text + '.xlsx')     
        })
    },
    onCancel: () => { }
  })
}

const saveFileFun = (code, text, listQuery, url,_time) => {
  console.log(code)
  switch (+code) {
    case 1:
      savePdfFn(text, listQuery, url,_time)
      break
    default:
      saveExcelFn(text, listQuery, url,_time)
      break
  }
}

export {
  saveFileFun
}
